package com.ngm.tea.tea.service.impl;

import javax.annotation.Resource;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import com.ngm.tea.common.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.ngm.tea.tea.dao.RatingsDao;
import com.ngm.tea.tea.entity.Ratings;
import com.ngm.tea.tea.service.RatingsService;

/**
 * 商品评论服务层
 * @author zjw
 * @version 1.0
 * @date 2017-01-03 15:51:34
 *
 */
@Service("ratingsService")
public class RatingsServiceImpl implements RatingsService {
	
	@Resource
	private RatingsDao ratingsDao;

	public RatingsDao getRatingsDao() {
		return ratingsDao;
	}

	@Override
	public void insert(Ratings ratings) {
		ratingsDao.insert(ratings);
	}

	@Override
	public void deleteById(Long id) {
		ratingsDao.deleteById(id);
	}

	@Override
	public void deleteByIds(String ids) {
		String[] _ids = ids.split(",");
		if (_ids.length > 0) {
			List<String> idlist = Arrays.asList(_ids);
			ratingsDao.deleteByIds(idlist);
		}
	}

	@Override
	public void delete(Ratings ratings) {
		ratingsDao.delete(ratings);
	}

	@Override
	public void update(Ratings ratings) {
		ratingsDao.update(ratings);
	}

	@Override
	public Ratings selectOne(Long id) {
		return ratingsDao.selectOne(id);
	}

	@Override
	public List<Ratings> selectAll() {
		return ratingsDao.selectAll();
	}

	@Override
	public List<Ratings> selectList(Ratings ratings, PageInfo pageInfo) {
		Map map = new HashMap<String, Object>();
		map.put("searchParams",makeStr(ratings));
		map.put("pageInfo", pageInfo);
		map.put("order", pageInfo.getOrderField()+" "+pageInfo.getOrderDirection());
		return ratingsDao.selectList(map);
	}

	@Override
	public Long count(Ratings ratings) {
		Map map = new HashMap<String, Object>();
		map.put("searchParams",makeStr(ratings));
		return ratingsDao.count(map);
	}
	
	public String makeStr(Ratings ratings) {
		StringBuffer sb = new StringBuffer();
		if(!StringUtils.isEmpty(ratings.getId())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("id LIKE '%" + ratings.getId()+"%'");
			} else {
				sb.append(" AND id LIKE '%" + ratings.getId()+"%'");
			}
		}
		if(!StringUtils.isEmpty(ratings.getRateType())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("rateType LIKE '%" + ratings.getRateType()+"%'");
			} else {
				sb.append(" AND rateType LIKE '%" + ratings.getRateType()+"%'");
			}
		}
		if(!StringUtils.isEmpty(ratings.getText())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("text LIKE '%" + ratings.getText()+"%'");
			} else {
				sb.append(" AND text LIKE '%" + ratings.getText()+"%'");
			}
		}
		if(!StringUtils.isEmpty(ratings.getUsername())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("username LIKE '%" + ratings.getUsername()+"%'");
			} else {
				sb.append(" AND username LIKE '%" + ratings.getUsername()+"%'");
			}
		}
		if(!StringUtils.isEmpty(ratings.getRateTime())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("rateTime LIKE '%" + ratings.getRateTime()+"%'");
			} else {
				sb.append(" AND rateTime LIKE '%" + ratings.getRateTime()+"%'");
			}
		}
		if(!StringUtils.isEmpty(ratings.getAvatar())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("avatar LIKE '%" + ratings.getAvatar()+"%'");
			} else {
				sb.append(" AND avatar LIKE '%" + ratings.getAvatar()+"%'");
			}
		}
		if (StringUtils.isEmpty(sb.toString())) {
			return null;
		} else {
			return sb.toString();
		}
	}

	@Override
	public List<Ratings> selectAllByGoodsId(Long fid) {
		return ratingsDao.selectAllByGoodsId(fid);
	}
	
}